home *** CD-ROM | disk | FTP | other *** search
- Path: news.itd.umich.edu!not-for-mail
- From: jlthomas@umd.umich.edu (Jeffrey L. Thomas)
- Newsgroups: comp.lang.c
- Subject: Re: Can't figure this out
- Date: 19 Jan 1996 18:25:09 -0500
- Organization: Univerisity of Michigan - Dearborn
- Message-ID: <4dp98l$in0@null.umd.umich.edu>
- References: <31000091.3778302@news.panix.com>
- NNTP-Posting-Host: null.umd.umich.edu
- NNTP-Posting-User: jlthomas
- X-Newsreader: NN version 6.5.0 #3 (NOV)
-
- dm@panix.com (Dan'l) writes:
-
- >how the outcome of this program is 15. Somehow one of the B's ends up
-
- >#define A 3
- >#define B A + A
- >#define C B * B
-
- >main()
- >{
- > printf("%d", C);
- > return 0;
- >}
-
- Ok Dan, I ran cpp (c pre-processor) over you bit of code and I think that
- the answer to you question is apparent now
-
- --
- # 1 "test.c"
-
- main() {
- printf("%d\n",3 + 3 * 3 + 3);
- exit(0);
- }
- --
- 3+3*3+3 => 3+9+3 => 15 the answer you got
-
- next you should try
-
- #define A (3)
- #define B ((A) + (A))
- #define C ((B) * (B))
-
- which produces C => (((3)) + ((3))) * (((3)) + ((3))) which gives you the
- answer that you seek, 36.
-
- The important lesson here is that #defines are very very literal in the text
- substitution, so you must always use parenthesis and plenty of them to
- avoid a mis-interpretation of you code by cpp.
- --
- \|||/ _Spike_Man_ ____ | Jeffery L. Thomas | "The important thing is
- ^.^ The Internet's / \ | jlthomas@umich.edu | not to stop questioning.
- v first superhero \ SM / | "This time it will | Curiosity has it own
- "more than a cute .sig" \__/ | surely run" - anon | reason for existing."
-